package com.jinlejia.bbs.ui.mine;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.InviteAdapter;
import com.jinlejia.bbs.bean.InviteBean;
import com.jinlejia.bbs.databinding.BbsActivityInviteAnswerBinding;
import com.jinlejia.bbs.help.CommonEvent;
import com.jinlejia.bbs.logic.InviteAnswerLogic;
import com.jinlejia.bbs.widget.recycler.PullInterface;

import java.util.ArrayList;

public class BBSInviteAnswerActivity extends AppCompatActivity implements InviteAnswerLogic.LogicCallBack, SwipeRefreshLayout.OnRefreshListener, PullInterface.LoadMoreListener {
    private BbsActivityInviteAnswerBinding binding;
    private ArrayList<InviteBean> dataList;
    private InviteAnswerLogic logic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.bbs_activity_invite_answer);
        binding.setCommonEvent(CommonEvent.obtain());
        dataList = new ArrayList<>();
        logic = new InviteAnswerLogic(this,dataList,this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }

    private InviteAdapter adapter;
    private void initView() {
        if (adapter==null){
            adapter = new InviteAdapter(this);
            adapter.setIgnoreListener(new InviteAdapter.IgnoreListener() {
                @Override
                public void onIgnoreSuccess() {
                    onRefresh();
                }

            });
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            binding.recyclerView.setAdapter(adapter);
            binding.recyclerView.setLayoutManager(layoutManager);
            binding.swipeRefreshLayout.setOnRefreshListener(this);
            binding.recyclerView.setAutoLoadMoreListener(this);
        }
        binding.swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                binding.swipeRefreshLayout.setRefreshing(true);
                logic.refresh();
            }
        });
    }

    @Override
    public void onResult(boolean hasNextPage) {
        adapter.initData(dataList);
        if (!hasNextPage){
            binding.recyclerView.setAutoLoadMoreListener(null);
        }else {
            binding.recyclerView.setAutoLoadMoreListener(this);
        }
        binding.recyclerView.getAdapter().notifyDataSetChanged();
        binding.swipeRefreshLayout.setRefreshing(false);

    }

    @Override
    public void onRefresh() {
        binding.swipeRefreshLayout.setRefreshing(true);
        logic.refresh();
    }

    @Override
    public void onLoadMore() {
        binding.swipeRefreshLayout.setRefreshing(true);
        logic.load();
    }
}
